package com.epam.giorgadze.task3.parsers;

import com.epam.giorgadze.task3.AirCompanyVariable;
import com.epam.giorgadze.task3.countries.Country;
import com.epam.giorgadze.task3.entity.AirCompany;
import com.epam.giorgadze.task3.entity.Manufacturer;
import com.epam.giorgadze.task3.entity.PlaneModel;
import com.epam.giorgadze.task3.entity.plane_type.CargoPlane;
import com.epam.giorgadze.task3.entity.plane_type.PassengerPlane;
import com.epam.giorgadze.task3.entity.plane_type.Plane;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: Екатерина
 * Date: 13.03.14
 * Time: 9:34
 * To change this template use File | Settings | File Templates.
 */
public class AirCompanySAXParser implements ContentHandler {

    private AirCompany airCompany = null;
    private List<Plane> planeList = new ArrayList<Plane>();
    private Plane plane = null;
    private PlaneModel planeModel = null;
    private Manufacturer manufacturer = null;
    private AirCompanyVariable airCompanyVariable = null;
    private Boolean isacn;

    public List<Plane> getPlaneList() {
        return planeList;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if (qName.equals("aircompany")) {
            airCompany = new AirCompany();
        }
        if (qName.equals("plane")) {
            if (atts.getValue(0).equals("CargoPlane")) {
                plane = new CargoPlane();
            } else if (atts.getValue(0).equals("PassengerPlane")) {
                plane = new PassengerPlane();
            }
        }
        if (qName.equals("planemodel")) {
            planeModel = new PlaneModel();
            planeModel.setName(atts.getValue("planeModelName"));
        }
        if (qName.equals("manufacturer")) {
            manufacturer = new Manufacturer();
        }
        if (!"aircompany".equals(qName) && !"plane".equals(qName) && !"planemodel".equals(qName) && !"manufacturer".equals(qName))
            airCompanyVariable = AirCompanyVariable.valueOf(qName.toUpperCase());

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("planemodel")) {
            planeModel.setManufacturer(manufacturer);
        }
        if (qName.equals("plane")) {
            plane.setModel(planeModel);
            planeList.add(plane);
        }
        if (qName.equals("aircompany")) {
            airCompany.setPlanes(planeList);
        }
        airCompanyVariable = null;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String s = new String(ch, start, length).trim();
        if (airCompanyVariable == null) {
            return;
        }
        switch (airCompanyVariable) {
            case AIRCOMPANYNAME:
                airCompany.setAirCompanyName(s);
                break;
            case AIRCOMPANYID:
                airCompany.setAirCompanyID(s);
                break;
            case TRAVELINGDISTANCE:
                plane.setTravelingDistance(Double.valueOf(s));
                break;
            case FUELCONSUMPTION:
                plane.setFuelConsumption(Double.valueOf(s));
                break;
            case PRICE:
                plane.setPrice(Double.valueOf(s));
                break;
            case LIFESPAN:
                plane.setLifespan(Double.valueOf(s));
                break;
            case ENGINECAPACITY:
                planeModel.setEngineCapacity(Double.valueOf(s));
                break;
            case WINGSPAN:
                planeModel.setWingspan(Double.valueOf(s));
                break;
            case PLANELENGTH:
                planeModel.setPlaneLength(Double.valueOf(s));
                break;
            case COURSESPEED:
                planeModel.setCourseSpeed(Double.valueOf(s));
                break;
            case FLIGHTLEVEL:
                planeModel.setFlightLevel(Double.valueOf(s));
                break;
            case COUNTRY:
                manufacturer.setCountry(Country.valueOf(s));
                break;
            case EMAIL:
                manufacturer.setEmail(s);
                break;
            case ADDRESS:
                manufacturer.setAddress(s);
                break;
            case PHONENUMBER:
                manufacturer.setPhoneNumber(Integer.valueOf(s));
                break;
            case NAME:
                manufacturer.setName(s);
                break;
            case TONNAGE:
                if (plane instanceof CargoPlane) ((CargoPlane) plane).setTonnage(Double.valueOf(s));
                break;
            case NUMBEROFSEATS:
                if (plane instanceof PassengerPlane) ((PassengerPlane) plane).setNumberOfSeats(Integer.valueOf(s));
                break;
        }
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
    }

    @Override
    public void processingInstruction(String target, String data) throws SAXException {
    }

    @Override
    public void skippedEntity(String name) throws SAXException {
    }


    @Override
    public void setDocumentLocator(Locator locator) {
    }

    @Override
    public void startDocument() throws SAXException {
    }

    @Override
    public void endDocument() throws SAXException {
    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
    }

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {
    }

    public AirCompany getAirCompany() {
        return airCompany;
    }
}


